package com.projectzqjz.parttimework.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.projectzqjz.parttimework.ui.fragment.PartMineFragment;
import com.projectzqjz.parttimework.ui.fragment.PartMsgFragment;
import com.projectzqjz.parttimework.ui.fragment.PopularPartTimeFragment;
import com.projectzqjz.parttimework.utils.StatusBarUtil;
import com.projectzqjz.parttimework.R;
import com.projectzqjz.parttimework.base.BaseActivity;
import com.projectzqjz.parttimework.entity.MainTabEntity;
import com.projectzqjz.parttimework.utils.UserUtils;
import com.projectzqjz.parttimework.widget.CenterDialog;


import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


import butterknife.BindView;

import static com.projectzqjz.parttimework.App.activities;

public class MainActivity extends BaseActivity {

    @BindView(R.id.tv_bg)
    TextView tv_bg;

    long lastClickTime = 0;//判断是否快速点击
    private CommonTabLayout mTabLayout;
    ArrayList<Fragment> mFragments = new ArrayList<>();
    private int currentTab = 0;
    private List<String> list = new ArrayList<>();

    @Override
    public void setTitleBarColor() {
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void onStart() {
        super.onStart();
        switch (mTabLayout.getCurrentTab()) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                EventBus.getDefault().post("checkLogin");
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void initData() {
        activities.add(this);
        String[] mTitles = {getString(R.string.msg), getString(R.string.contact), getString(R.string.my)};
        int[] mIconUnSelectIds = {
                R.drawable.icon_home_unselected, R.drawable.icon_chosen_unselected,
                R.drawable.icon_mine_unselected};
        int[] mIconSelectIds = {
                R.drawable.icon_home_selected, R.drawable.icon_chosen_selected,
                R.drawable.icon_mine_selected};
        //tab的标题、选中图标、未选中图标
        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        //设置tab的标题、选中图标、未选中图标
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new MainTabEntity(mTitles[i], mIconSelectIds[i], mIconUnSelectIds[i]));
        }
        //初始化数据
        mFragments.add(new PopularPartTimeFragment());
        mFragments.add(new PartMsgFragment(tv_bg));
        mFragments.add(new PartMineFragment());
        //给tab设置数据和关联的fragment
        mTabLayout.setTabData(mTabEntities, this, R.id.fl_change, mFragments);
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (position) {
                    case 0:
                        StatusBarUtil.setStatusBar(MainActivity.this, true);
                        break;
                    case 1:
                        StatusBarUtil.setStatusBar(MainActivity.this, true);
                        break;
                    case 2:
                        StatusBarUtil.setStatusBar(MainActivity.this, true);
                        break;
                }
                if (!UserUtils.checkLogin() && position != 2) {//用户未登录且不在个人页，记录当前页
                    currentTab = position;
                }
            }

            @Override
            public void onTabReselect(int position) {
                if (!UserUtils.checkLogin() && position != 2) {//用户未登录且不在个人页，记录当前页
                    currentTab = position;
                }
            }
        });
    }

    @Override
    public void initView() {
        //初始化组件
        mTabLayout = findViewById(R.id.tl);


//        初始化QQ分享
//        myIUiListener = new MyIUiListener();
    }

    @Override
    public void setListen() {
    }

    /**
     * 处理快速点击造成的重复生成界面影响
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (isFastDoubleClick()) {
                return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        int tab = intent.getIntExtra("tab", 0);
        switch (tab) {
            case 0:
            case 2:
                mTabLayout.setCurrentTab(tab);
                break;
            case 1:
                String categoryId = intent.getStringExtra("actionUrl");
                toCategory(categoryId);
                break;
        }
    }

    /**
     * 判断是否快速点击(时间间隔在300ms以内，就算快速点击)
     *
     * @return
     */
    private boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        lastClickTime = time;
        return timeD <= 300;
    }

    /**
     * 获取底部控件
     *
     * @return
     */
    public CommonTabLayout getTabLayout() {
        return mTabLayout;
    }

    /**
     * 获取当前页
     */
    public int getCurrentTab() {
        return currentTab;
    }

    /**
     * 跳转到分类页面
     *
     * @param categoryId
     */
    private void toCategory(String categoryId) {
        mTabLayout.setCurrentTab(1);
        Bundle bundle = new Bundle();
        bundle.putString("categoryId", categoryId);
        if (mFragments.get(1) != null) {
            mFragments.get(1).setArguments(bundle);
        }
    }

    /**
     * 拦截返回键
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK://返回键
                new CenterDialog(R.layout.prompt_quit_dialog, MainActivity.this).showQuitPrompt("确认要退出应用？");
                break;
            case KeyEvent.KEYCODE_MENU://菜单键
                break;
            case KeyEvent.KEYCODE_HOME://home键
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}

